package com.gmail.bloim.serg.acmbuilder;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

/**
 * User: Администратор
 * Date: 26.04.13
 * Time: 13:18
 */
public class JavaMerger {
    public String merge(Collection<JavaFile> javaFiles) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        PrintStream printer = new PrintStream(out);
        Set<Import> imports = new HashSet<Import>();
        for(JavaFile javaFile : javaFiles) {
            Set<Import> fileImports = javaFile.getFileImports();
            imports.addAll(fileImports);
        }
        for(Import anImport : imports) {
            printer.println(anImport.getImportStr());
        }
        printer.println();
        for(JavaFile javaFile : javaFiles) {
            printer.println(javaFile.getContent());
        }
        return out.toString();
    }
}
